<?php
App::uses('AppModel', 'Model');
/**
 * Projeto Model
 *
 * @property Empresas $Empresas
 * @property User $User
 */
class Projeto extends AppModel {
/**
 * Primary key field
 *
 * @var string
 */
	public $primaryKey = 'id_projetos';
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'nome';
/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'id_projetos' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'empresas_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'nome' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'descricao' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Empresa' => array(
			'className' => 'Empresa',
			'foreignKey' => 'empresas_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
	public $hasAndBelongsToMany = array(
		'User' => array(
			'className' => 'User',
			'joinTable' => 'projetos_users',
			'foreignKey' => 'projetos_id',
			'associationForeignKey' => 'users_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
	
	public function usuariosDoProjetoEmail($idProjeto, $usuario){
		$usuarios = $this->usuariosDoProjeto($idProjeto);
		foreach ($usuarios as $item){
			if($usuario['id_users'] != $item['id_users'])
			$emails[$item['email']] = $item['nome'];
		}
		return $emails;
	}
	
	public function usuariosDoProjeto($idProjeto){
		$this->recursive = 1;
		$dados = $this->findByIdProjetos($idProjeto);
		return $dados['User'];
	}
	
	public function novasMensagens($id, $idMensagem, $idTarefa, $usuario){
		$dados = $this->findByIdProjetos($id);
		$naoLido = ClassRegistry::init("NaoLido");
		foreach($dados['User'] as $item){
			if($item['id_users'] != $usuario['id_users']){
				$naoLido->create();
				$naoLido->save(array('NaoLido'=>array('users_id'=>$item['id_users'], 'mensagens_id'=>$idMensagem, 'tarefas_id'=>$idTarefa)));
			}
		}
		
	}

}
